package com.example.mvcdemo1.service;

import com.example.mvcdemo1.model.Student;
import com.example.mvcdemo1.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.Option;
import java.util.List;
import java.util.Optional;

@Service
public class StudentServiceLmpl implements StudentService{
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }

    @Override
    public void saveStudent(Student student) {
        this.studentRepository.save(student);

    }

    @Override
    public Student getStudentByid(Long id) {
        Optional<Student> optional = studentRepository.findById(id);
        Student student = null;
        if( optional.isPresent()){
            student = optional.get();
        }else{
            throw new RuntimeException(" 查无此人，请重输入, id :  "+ id);
        }
        return student;
    }


    @Override
    public void deleteStudentByid(Long id) {
        this.studentRepository.deleteById(id);

    }
}
